//Global variable, used to store a reference to the Flyout-Silverlight-object
var flyoutSL = null;

function delegate(o,m){
    return function(){
        return m.apply(o,arguments)
    }
}

if (!window.UntitledProject1)
MSNWidget = {};

MSNWidget.Page = function()
{

}

MSNWidget.Page.prototype =
{
    handleLoad: function(control, userContext, rootElement) {
        
        document.body.style.width = 135;
        
        MSNWidget.platformConnector = new PlatformConnector();
        MSNWidget.platformConnector.setFlyoutFile("flyout.html");
        MSNWidget.platformConnector.onFlyoutShow(function() {
            if(Weather.visible)
                Flyout.showWeather();
            else
                Flyout.addContentToFlyout();
        });
    
        MSNWidget.platformConnector.onSettingsClosed(GUI.Bar.inkBar);
        MSNWidget.platformConnector.onUndock(GUI.undocked);
        MSNWidget.platformConnector.onDock(GUI.docked);
        MSNWidget.platformConnector.settingsUI("settings.html");
        var result = MSNWidget.platformConnector.loadSettings("SettingsPresent", "bool");
        var save_str = "";
        if (result == (false || "") ) {
            MSNWidget.platformConnector.saveSettings("bgColor", 0);
            
            for(i = 0; i < locale.categories.length; i++){
                save_str += i + "-";
            }
            save_str = save_str.substring(0, save_str.length-1);
            MSNWidget.platformConnector.saveSettings("categories", save_str);
        }

        MSNWidget.control = control;
        MSNWidget.rootElement = rootElement;

        MSNWidget.root = rootElement;
        MSNWidget.flyout = false;
        MSNWidget.categories = new Array();
                
        //Die Kategorien in der richtigen Reihenfolge einlesen
        if(MSNWidget.platformConnector.loadSettings("categories") == false)
            cats = save_str.split("-");
        else
            cats = MSNWidget.platformConnector.loadSettings("categories").split("-");
            
        for(i = 0; i < cats.length; i++){
            MSNWidget.categories.push(locale.categories[parseInt(cats[i])]);
        }
        
        MSNWidget.preview = new Array();
        MSNWidget.preview_img = new Array();
        MSNWidget.preview_date = new Array();
        MSNWidget.preview_title = new Array();
        MSNWidget.preview_link = new Array();
        MSNWidget.news = 0;
        MSNWidget.newsCat = 101;
        MSNWidget.downloaded_images = 0;
        MSNWidget.total_images = 0;
    
        GUI.update();
        
        WeatherSearchBar_init();
        Scrollbar_Init();
        Weather.init();
        rootElement.findName("SearchBar_hidden").Focus();
        
        MSNWidget.weatherSearchBar = new WeatherSearchBar();
        MSNWidget.weatherSearchBar.init(MSNWidget.root);        
                
        MSNWidget.token_me_l = null;
        MSNWidget.token_me_r = null;
        MSNWidget.token_ml_l = null;
        MSNWidget.token_ml_r = null;
        MSNWidget.token_md_l = null;
        MSNWidget.token_md_r = null;
        MSNWidget.token_flyout = null;
        
        rootElement.addEventListener("LostFocus", GUI.handleLostFocus);
        rootElement.addEventListener("MouseMove", GUI.Bar.Events.handleMouseMoveRoot);
        rootElement.addEventListener("MouseLeave", GUI.Content.Events.handleMouseLeave);
        rootElement.findName("Bar").addEventListener("MouseMove", GUI.Bar.Events.handleMouseMoveBar);
        rootElement.findName("Bar").addEventListener("MouseLeave", GUI.Bar.Events.handleMouseLeaveBar);
        rootElement.findName("More").addEventListener("MouseMove", GUI.Bar.Events.handleMouseMoveBar);
        rootElement.findName("More").addEventListener("MouseLeave", GUI.Bar.Events.handleMouseLeaveBar);
                
        if (MSNWidget.platformConnector.loadSettings("SettingsPresent", "bool"))
            MSNWidget.newsCat = parseInt(MSNWidget.platformConnector.loadSettings("newsCat"));
                  
        GUI.Bar.addEvents(rootElement.findName("MSN-Startseite"));
        GUI.Bar.addEvents(rootElement.findName("subcat_text"));
        
        rootElement.findName("subcat_main_left_btn").addEventListener("MouseLeftButtonDown", GUI.Bar.switchMainCat);
        rootElement.findName("subcat_main_right_btn").addEventListener("MouseLeftButtonDown", GUI.Bar.switchMainCat);

        GUI.buildSubcats(rootElement, MSNWidget.newsCat);
        
        SearchBar.init();
        
        MSNWidget.rootElement.addEventListener("MouseLeftButtonDown", GUI.looseFocus);
        rootElement.findName("SearchBar_Options").addEventListener("MouseLeftButtonDown", GUI.showOptions);
        rootElement.findName("SearchBar_Usage").addEventListener("MouseLeftButtonDown", GUI.showUsage);
        rootElement.findName("SearchBar_Download").addEventListener("MouseLeftButtonDown", GUI.showDownload);
        GUI.Bar.addEvents(rootElement.findName("Btn_MSN"));
        GUI.Bar.addEvents(rootElement.findName("Btn_More"));

                
        if(MSNWidget.platformConnector.isDocked())
            GUI.docked();
        else GUI.undocked();
            GUI.undocked();
        
        SearchBar.showButtons(!MSNWidget.platformConnector.isDocked());
        window.focus();
        
    }
    
}